package com.itfitness.ftlib.widget.tabview;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.itfitness.ftlib.R;

import java.util.List;

/**
 * @ProjectName: FitnessLib
 * @Package: com.itfitness.ftlib.widget.tabview
 * @ClassName: FTabView
 * @Description: java类作用描述 自定义底部导航
 * @Author: 作者名
 * @CreateDate: 2020/11/22 9:12
 * @UpdateUser: 更新者：itfitness
 * @UpdateDate: 2020/11/22 9:12
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class FTabView extends FrameLayout {
    private LinearLayout llTabContainer;
    private LinearLayout.LayoutParams itemLayoutParams;
    private LinearLayout llBigImageTabContainer;
    private LinearLayout.LayoutParams plaItemLayoutParams;

    public FTabView(@NonNull Context context) {
        this(context,null);
    }

    public FTabView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FTabView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //设置控件高度自适应，宽度填充父布局
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        init();
    }

    private void init() {
        initTabContainer();
        initItemLayoutParams();
    }

    private void initItemLayoutParams() {
        itemLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        itemLayoutParams.weight = 1;
        itemLayoutParams.topMargin = 10;
        itemLayoutParams.bottomMargin = 10;

        //占位ItemView的LayoutParams
        plaItemLayoutParams = new LinearLayout.LayoutParams(0, 1);
        plaItemLayoutParams.weight = 1;
        plaItemLayoutParams.topMargin = 10;
        plaItemLayoutParams.bottomMargin = 10;
    }

    /**
     * 加载Tab容器
     */
    private void initTabContainer() {
        LayoutParams containerLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        containerLayoutParams.gravity = Gravity.BOTTOM;
        llTabContainer = new LinearLayout(getContext());
        llTabContainer.setOrientation(LinearLayout.HORIZONTAL);
        llTabContainer.setGravity(Gravity.CENTER_VERTICAL);
        llTabContainer.setLayoutParams(containerLayoutParams);
        llTabContainer.setBackgroundColor(Color.WHITE);

        //大图的容器
        llBigImageTabContainer = new LinearLayout(getContext());
        llBigImageTabContainer.setOrientation(LinearLayout.HORIZONTAL);
        llBigImageTabContainer.setGravity(Gravity.CENTER_VERTICAL);
        llBigImageTabContainer.setLayoutParams(containerLayoutParams);
        addView(llTabContainer);
        addView(llBigImageTabContainer);
    }

    public void setDatas(List<? extends FTabBaseItemBean> tabDatas,int defaultPos){
        llTabContainer.removeAllViews();
        llBigImageTabContainer.removeAllViews();
        for(int i = 0 ; i < tabDatas.size() ; i++){
            FTabBaseItemBean tabBean = tabDatas.get(i);
            FTabBaseItemView fTabItemView;
            if(tabBean.isBigImage()){
                fTabItemView = new FTabBigImageItemView(getContext());
            }else {
                fTabItemView = new FTabItemView(getContext());
            }
            fTabItemView.setData(tabDatas.get(i));
            if(i == defaultPos){
                fTabItemView.setSelect(true);
            }else {
                fTabItemView.setSelect(false);
            }
            fTabItemView.setLayoutParams(itemLayoutParams);
            final int finalI = i;
            fTabItemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateStatus(finalI);
                }
            });

            //占位符View
            View view = new View(getContext());
            view.setLayoutParams(plaItemLayoutParams);

            if(tabBean.isBigImage()){
                llBigImageTabContainer.addView(fTabItemView);
                llTabContainer.addView(view);
            }else {
                llTabContainer.addView(fTabItemView);
                llBigImageTabContainer.addView(view);
            }
        }
    }

    /**
     * 更新状态
     * @param selectPos
     */
    private void updateStatus(int selectPos){
        for(int  i = 0 ; i < llTabContainer.getChildCount() ; i ++){
            if(llTabContainer.getChildAt(i) instanceof FTabBaseItemView){
                FTabBaseItemView fTabItemView = (FTabBaseItemView) llTabContainer.getChildAt(i);
                fTabItemView.setSelect(selectPos == i);
            }
        }
        for(int  i = 0 ; i < llBigImageTabContainer.getChildCount() ; i ++){
            if(llBigImageTabContainer.getChildAt(i) instanceof FTabBaseItemView){
                FTabBaseItemView fTabItemView = (FTabBaseItemView) llBigImageTabContainer.getChildAt(i);
                fTabItemView.setSelect(selectPos == i);
            }
        }
    }
}
